#lang racket


(require "../base/main.rkt")
(require "../config/main.rkt")

(require "constructor-imediate.rkt")

(provide render
         render-fn)


(define (render-impl shape view-fn file dir ext width height)
  (begin0
    (evaluate shape)
    (let ((path (format "~A~A.~A" (path->string dir) file ext)))
      (displayln (format "Render (~Ax~A) ~A" width height path))
      (view-fn)
      (render-shape width height path))))

(define (render #:dir (dir (render-dir))
                #:ext (ext (render-ext))
                #:width (width (render-width))
                #:height (height (render-height))
                . args)
  (match args
    ((list shape file)
     (render-impl shape view file dir ext width height))
    ((list shape file view-fn)
     (render-impl shape view-fn file dir ext width height))
    ((list shape file center target)
     (render-impl shape (cλ view center target) file dir ext width height))
    ((list shape file center target lens)
     (render-impl shape (cλ view center target lens) file dir ext width height))))

(define (render-fn #:dir (dir (render-dir))
                   #:ext (ext (render-ext))
                   #:width (width (render-width))
                   #:height (height (render-height))
                   . args)
  (define (fn-name fn)
    (let* ((name (object-name fn))
           (file (symbol->string name)))
      (if name
          file
          (error 'render-fn (format "Render-fn: first parameter (fn) must not be an anonymous function, given ~A; for anonymous functions use 'render' instead" fn)))))
  (match args
    ((list fn)
     (render-impl (fn) view (fn-name fn) dir ext width height))
    ((list fn view-fn)
     (render-impl (fn) view-fn (fn-name fn) dir ext width height))
    ((list fn center target)
     (render-impl (fn) (cλ view center target) (fn-name fn) dir ext width height))
    ((list fn center target lens)
     (render-impl (fn) (cλ view center target lens) (fn-name fn) dir ext width height))))